package 第三周;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xhw
 */

public class 电话号码的字母组合 {
    public static void main(String[] args) {
        String digits = "23";
        System.out.println(letterCombinations(digits));
    }

    private static List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        if (digits == null || digits.isEmpty()) {
            return combinations;
        }

        int index = 0;
        String[] mapping = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        combinations.add("");

        while (index < digits.length()) {
            int digit = Character.getNumericValue(digits.charAt(index));
            while (combinations.get(0).length() == index) {
                String permutation = combinations.remove(0);
                for (char letter : mapping[digit].toCharArray()) {
                    combinations.add(permutation + letter);
                }
            }
            index++;
        }

        return combinations;
    }
}
